package com.baijiayun.liveuibase.widgets.toolbar;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.baijiayun.liveuibase.R;
import com.baijiayun.liveuibase.utils.DisplayUtils;

import java.util.HashMap;

public class TextEditFrameLayout {
    private final View view;
    private final EditText editText;
    private String beforeText = "";
    private final TextWatcher textWatcher;

    @SuppressLint("InflateParams")
    public TextEditFrameLayout(Context context, IOnTextChangeListener onTextChangeListener) {
        view = LayoutInflater.from(context).inflate(R.layout.uibase_text_edit_layout, null);

        editText = view.findViewById(R.id.base_message_send_et);
        LinearLayout send = view.findViewById(R.id.base_message_send_btn);
        send.setOnClickListener(v -> {
            HashMap<String, String> map = new HashMap<>();
            map.put("beforeText", beforeText);
            map.put("afterText", editText.getEditableText().toString());
            // end 为 true 时，结束输入，需要 1.移除当前 view；2. 发送事件
            map.put("end", "true");
            if (onTextChangeListener != null) {
                onTextChangeListener.OnTextChange(map);
            }
            setEditText("");
            DisplayUtils.hideKeyboard(v);
        });
        view.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
            @Override
            public void onViewAttachedToWindow(View v) {
                // 加入视图时，拉起键盘
                editText.setFocusable(true);
                editText.setFocusableInTouchMode(true);
                editText.requestFocus();
                editText.removeTextChangedListener(textWatcher);
                editText.setText(beforeText);
                editText.setSelection(editText.getText().length());
                editText.addTextChangedListener(textWatcher);
                editText.setSelection(editText.getText().length());
                DisplayUtils.showInputMethod(context);
            }

            @Override
            public void onViewDetachedFromWindow(View v) {
                if (context == null) return;
                DisplayUtils.hideKeyboard(v);
            }
        });

        textWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                beforeText = s.toString();
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // do nothing
            }

            @Override
            public void afterTextChanged(Editable s) {
                // 文本变更 发送通知
                HashMap<String, String> map = new HashMap<>();
                map.put("beforeText", beforeText);
                map.put("afterText", editText.getEditableText().toString());
                if (onTextChangeListener != null) {
                    onTextChangeListener.OnTextChange(map);
                }
            }
        };
        editText.addTextChangedListener(textWatcher);
    }

    public void setEditText(String text){
        if (editText.isAttachedToWindow()) {
            editText.removeTextChangedListener(textWatcher);
            editText.setText(text);
            //移动光标到末尾
            editText.setSelection(editText.getText().length());
            editText.addTextChangedListener(textWatcher);
        }
        beforeText = text;
    }

    public View getView() {
        return view;
    }

    public interface IOnTextChangeListener{
        void OnTextChange(HashMap<String, String> map);
    }
}
